package com.windward.vii.service;

import com.windward.vii.common.JsonResult;
import com.windward.vii.common.ResultCode;
import com.windward.vii.dao.entity.Device;
import com.windward.vii.dao.entity.UserDevice;
import com.windward.vii.dao.mapper.DeviceMapper;
import com.windward.vii.dao.mapper.UserDeviceMapper;
import com.windward.vii.utils.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class DeviceService {

    @Resource
    private UserDeviceMapper userDeviceMapper;
    @Resource
    private DeviceMapper deviceMapper;

    /**
     * 保存device信息
     */
    public JsonResult insertOrUpdateLoginDevice(Long uid, Device deviceParam) {
        // 判断是否为新设备
        Device dbDevice = null;
        if(StringUtils.isNotEmpty(deviceParam.getDeviceId())) {
            dbDevice = deviceMapper.selectByDeviceId(deviceParam.getDeviceId());
        }
        if (dbDevice == null) { // 新设备
            deviceParam.setRegisterTime(new Date());
            deviceParam.setRegisterUid(uid);
            deviceParam.setLoginUid(uid);
            if (deviceMapper.insertSelective(deviceParam) < 1) {
                return new JsonResult(ResultCode.DB_ERROR_CODE, "保存设备信息失败");
            }
            UserDevice userDevice = new UserDevice();
            userDevice.setDeviceId(deviceParam.getId());
            userDevice.setUid(uid);
            userDevice.setRegisterTime(new Date());
            userDevice.setLoginTime(new Date());
            if(userDeviceMapper.insert(userDevice) < 1) {
                return new JsonResult(ResultCode.DB_ERROR_CODE, "保存用户设备关系失败");
            }
        } else { // 数据库已存在设备
            Device updateDevice = new Device();
            updateDevice.setId(dbDevice.getId());
            updateDevice.setLoginUid(uid);
            if (deviceMapper.updateByPrimaryKeySelective(updateDevice) < 1) {
                return new JsonResult(ResultCode.DB_ERROR_CODE, "更新设备信息失败");
            }
            UserDevice dbUserDevice = userDeviceMapper.selectByUidAndDeviceId(uid, dbDevice.getId());
            // 是否已绑定user-device
            if(dbUserDevice == null) {
                UserDevice userDevice = new UserDevice();
                userDevice.setDeviceId(dbDevice.getId());
                userDevice.setUid(uid);
                userDevice.setRegisterTime(new Date());
                userDevice.setLoginTime(new Date());
                if(userDeviceMapper.insert(userDevice) < 1) {
                    return new JsonResult(ResultCode.DB_ERROR_CODE, "保存用户设备关系失败");
                }
            } else {
                UserDevice updateUserDevice = new UserDevice();
                updateUserDevice.setDeviceId(dbUserDevice.getId());
                updateUserDevice.setLoginTime(new Date());
                if(userDeviceMapper.updateByPrimaryKeySelective(updateUserDevice) < 1) {
                    return new JsonResult(ResultCode.DB_ERROR_CODE, "保存用户设备关系失败");
                }
            }
        }
        return JsonResult.success();
    }

//    private String genDeviceSign(Device device) {
//        StringBuffer sign = new StringBuffer();
//        sign.append(device.getImei()).append(device.getMac()).append(",")
//                .append(device.getSn()).append(device.getUuid()).append(",");
//        System.out.println(sign);
//        return MD5Util.md5Hex(sign.toString());
//    }
}
